package middleware

import (
	"context"
	"github.com/RaymondCode/simple-demo/config"
	"github.com/cloudwego/hertz/pkg/app"
	"github.com/timandy/routine"
	"strconv"
)

var ThreadLocal = routine.NewInheritableThreadLocal()

func SaveUserId(c context.Context, ctx *app.RequestContext) {
	var storage = make(map[string]string, 1)
	var userId = ctx.Keys[config.Config.ThreadLocal.Keys.UserId]
	if userId != nil{
		storage[config.Config.ThreadLocal.Keys.UserId] = strconv.Itoa(userId.(int))
		ThreadLocal.Set(storage)
		ctx.Next(c)
		ThreadLocal.Remove()
	}
	ctx.Next(c)
}
